import frappe
from frappe.model.meta import Meta,get_table_columns
from frappe.model import child_table_fields

custom_data_fieldtypes = (
	"Currency",
	"Int",
	"Long Int",
	"Float",
	"Percent",
	"Check",
	"Small Text",
	"Long Text",
	"Code",
	"Text Editor",
	"Markdown Editor",
	"HTML Editor",
	"Date",
	"Datetime",
	"Time",
	"Text",
	"Data",
	"Link",
    "Link MultiSelect",
	"Dynamic Link",
	"Password",
	"Select",
	"Rating",
	"Read Only",
	"Attach",
	"Attach Image",
	"Signature",
	"Color",
	"Barcode",
	"Geolocation",
	"Duration",
	"Icon",
	"Phone",
	"Autocomplete",
	"JSON",
)

def get_list_fields(self):
	list_fields = ["name"] + [
		d.fieldname for d in self.fields if (d.in_list_view and d.fieldtype in custom_data_fieldtypes)
	]
	if self.title_field and self.title_field not in list_fields:
		list_fields.append(self.title_field)
	return list_fields

def get_valid_columns(self):
	if not hasattr(self, "_valid_columns"):
		table_exists = frappe.db.table_exists(self.name)
		if self.name in self.special_doctypes and table_exists:
			self._valid_columns = get_table_columns(self.name)
		else:
			self._valid_columns = self.default_fields + [
				df.fieldname for df in self.get("fields") if df.fieldtype in custom_data_fieldtypes
			]
			if self.istable:
				self._valid_columns += list(child_table_fields)

	return self._valid_columns

Meta.get_list_fields = get_list_fields
Meta.get_valid_columns = get_valid_columns